package com.isnmp.nms.framework.discover.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.isnmp.common.base.MyLog;
import com.isnmp.common.snmp.mib.MibMacIP;
import com.isnmp.nms.framework.discover.DeviceInfo;
import com.isnmp.nms.framework.discover.DiscoverConfig;
import com.isnmp.nms.framework.discover.PortInfo;

public class DeviceInfoManager {
    public List<DeviceInfo> allDeviceList = new Vector();
    public List<DeviceInfo> switchList = new Vector();
    public HashMap<String, DeviceInfo> macDeviceMap = new HashMap();
    public Map<String, String> allMacIpTable = new HashMap();

    public DeviceInfoManager(List<DeviceInfo> paramList) {
        MyLog.goodMessage(Messages.getString("DeviceInfoManager.0") + new Timestamp(System.currentTimeMillis()));
        this.allDeviceList = paramList;
        initMacInfo();
        initDeviceGroupInfo();
        initPortSubDevices();
        MyLog.goodMessage(Messages.getString("DeviceInfoManager.1") + new Timestamp(System.currentTimeMillis()));
    }

    private void initMacInfo() {
        Iterator localIterator1 = this.allDeviceList.iterator();
        Object localObject2;
        while (localIterator1.hasNext()) {
            Object localObject1 = (DeviceInfo) localIterator1.next();
            localObject2 = ((DeviceInfo) localObject1).getDeviceMAC();
            List localList1 = ((DeviceInfo) localObject1).getDeviceMacList();
            List localList2 = ((DeviceInfo) localObject1).getMibMacIPList();
            Iterator localIterator2 = localList2.iterator();
            Object localObject3;
            while (localIterator2.hasNext()) {
                localObject3 = (MibMacIP) localIterator2.next();
                this.allMacIpTable.put(((MibMacIP) localObject3).getIpNetToMediaPhysAddress(), ((MibMacIP) localObject3).getIpNetToMediaNetAddress());
            }
            localIterator2 = localList1.iterator();
            while (localIterator2.hasNext()) {
                localObject3 = (String) localIterator2.next();
                //                this.allMacIpTable.put(localObject3, ((DeviceInfo) localObject1).getDeviceIP());
                //                this.macDeviceMap.put((String) localObject3, localObject1);
                this.allMacIpTable.put(((MibMacIP) localObject3).getMappingOID(), ((DeviceInfo) localObject1).getDeviceIP());
                this.macDeviceMap.put((String) localObject3, (DeviceInfo) localObject1);
            }
            if (!((String) localObject2).equals("")) {
                //                this.allMacIpTable.put(localObject2, ((DeviceInfo) localObject1).getDeviceIP());
                //                this.macDeviceMap.put(localObject2, localObject1);
                this.allMacIpTable.put(((MibMacIP) localObject2).getMappingOID(), ((DeviceInfo) localObject1).getDeviceIP());
                this.macDeviceMap.put((String) localObject2, (DeviceInfo) localObject1);
            }
        }
        MyLog.goodMessage(Messages.getString("DeviceInfoManager.3"));
        Object localObject1 = this.allMacIpTable.entrySet();
        localIterator1 = ((Set) localObject1).iterator();
        while (localIterator1.hasNext()) {
            localObject2 = (Map.Entry) localIterator1.next();
            MyLog.goodMessage(Messages.getString("DeviceInfoManager.4") + (String) ((Map.Entry) localObject2).getKey() + Messages.getString("DeviceInfoManager.5") + (String) ((Map.Entry) localObject2).getValue());
        }
    }

    private void initDeviceGroupInfo() {
        MyLog.goodMessage(Messages.getString("DeviceInfoManager.6"));
        try {
            Iterator localIterator = this.allDeviceList.iterator();
            while (localIterator.hasNext()) {
                DeviceInfo localDeviceInfo = (DeviceInfo) localIterator.next();
                if (localDeviceInfo.getdTpFdbTableList().size() > 0) {
                    MyLog.goodMessage(localDeviceInfo.getDeviceIP() + Messages.getString("DeviceInfoManager.7"));
                    this.switchList.add(localDeviceInfo);
                } else {
                    System.out.println(localDeviceInfo.getDeviceIP() + Messages.getString("DeviceInfoManager.8"));
                }
            }
            System.out.println(Messages.getString("DeviceInfoManager.9") + this.switchList.size() + Messages.getString("DeviceInfoManager.10"));
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    private void initPortSubDevices() {
        try {
            System.out.println("=============================");
            System.out.println(Messages.getString("DeviceInfoManager.12"));
            Iterator localIterator1 = this.switchList.iterator();
            while (localIterator1.hasNext()) {
                DeviceInfo localDeviceInfo = (DeviceInfo) localIterator1.next();
                if (DiscoverConfig.isDebug)
                    System.out.println("=============================");
                Iterator localIterator2 = localDeviceInfo.getPortInfoList().iterator();
                while (localIterator2.hasNext()) {
                    PortInfo localPortInfo = (PortInfo) localIterator2.next();
                    initPortInfo(localPortInfo);
                }
            }
            System.out.println(Messages.getString("DeviceInfoManager.14"));
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public DeviceInfo getDeviceInfoFromDeviceListByIp(String paramString) {
        Iterator localIterator = this.allDeviceList.iterator();
        while (localIterator.hasNext()) {
            DeviceInfo localDeviceInfo = (DeviceInfo) localIterator.next();
            if (localDeviceInfo.getDeviceIP().equals(paramString))
                return localDeviceInfo;
        }
        return null;
    }

    public DeviceInfo getDeviceInfoByMAC(String paramString) {
        if ((paramString == null) || (paramString.equals("")))
            return null;
        return (DeviceInfo) this.macDeviceMap.get(paramString);
    }

    public String getIPOfMac(String paramString) {
        return (String) this.allMacIpTable.get(paramString);
    }

    private void initPortInfo(PortInfo paramPortInfo) {
        List localList = paramPortInfo.portMacList;
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext()) {
            String str = (String) localIterator.next();
            DeviceInfo localDeviceInfo = getDeviceInfoByMAC(str);
            if ((localDeviceInfo != null) && (!localDeviceInfo.equals(paramPortInfo.device)) && (!paramPortInfo.subDeviceList.contains(localDeviceInfo))) {
                paramPortInfo.subDeviceList.add(localDeviceInfo);
                if (DiscoverConfig.isDebug)
                    System.out.println(paramPortInfo + Messages.getString("DeviceInfoManager.16") + localDeviceInfo);
            }
        }
    }
}